var _ = require('lodash');
var path = require("path");
var dao = require(path.join(process.cwd(),"dao/DAO"));
/**
 * 获取所有文章
 *
 * @param  {[type]}   type    描述显示层级
 * @param  {Function} cb      回调函数
 */
module.exports.getAllCategories = function(type,conditions,cb) {
    dao.list("CategoryModel",{"cat_deleted":false},function(err,categories) {
        var keyCategories = _.keyBy(categories,'cat_id');
        if(!type) type = 3;

        result = getTreeResult(keyCategories,categories,type);

        if(conditions) {
            count = result.length;
            pagesize = parseInt(conditions.pagesize);
            pagenum = parseInt(conditions.pagenum) - 1;
            result = _.take(_.drop(result,pagenum * pagesize),pagesize)
            var resultDta = {};
            resultDta["total"] = count;
            resultDta["pagenum"] = pagenum;
            resultDta["pagesize"] = pagesize;
            resultDta["result"] = result;
            return cb(null,resultDta);
        }
        cb(null,result);
    });
}
